package com.mls.fivedistrict.controller.backend;

import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Integral;
import com.mls.fivedistrict.server.IntegralService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by nanji on 2018/3/20.
 */
@RestController
@RequestMapping("/api/manager")
public class IntegralController extends BaseController {
    @Resource(name = "integralService")
    IntegralService integralService;

    /**
     * 获取积分的内容，为下拉列表提供接口
     * @return
     */
    @GetMapping("/v1/integralIdAndName")
    public ServiceResponse getIntegralIdAndName() {
        List<Integral> integralList = integralService.getIntegralListForTask();
        return ServiceResponse.createSuccessResponse(integralList);
    }

    @GetMapping("/v1/integrals")
    public ServiceResponse getIntegralList() {
        PageFilter pageFilter = this.getPageFilter();
        return integralService.getIntegralList(pageFilter);
    }

    @DeleteMapping("/v1/integral/{integralId}")
    public ServiceResponse deleteIntegral(@PathVariable("integralId") Long id) {
        return integralService.deleteIntegral(id);
    }

    @PostMapping("/v1/integral")
    public ServiceResponse addIntegral(@RequestBody Integral integral) {
//        Optional.ofNullable(integral.getValidity()).orElseThrow(() -> new CustomerException("有效期未添加"));
//        Optional.ofNullable(integral.getFrequencyId()).orElseThrow(() -> new CustomerException("积分产出频率对应的ID字段未添加"));
        Optional.ofNullable(integral.getLogo()).orElseThrow(() -> new CustomerException("logo字段未添加"));
//        Optional.ofNullable(integral.getMaxWithdrawNumber()).orElseThrow(() -> new CustomerException("最大提现额度字段未添加"));
//        Optional.ofNullable(integral.getMinParticipationCalculate()).orElseThrow(() -> new CustomerException("最小参与这个挖矿的计算能力字段未添加"));
//        Optional.ofNullable(integral.getMinWithdrawNumber()).orElseThrow(() -> new CustomerException("最小提现额度字段未添加"));
        Optional.ofNullable(integral.getName()).orElseThrow(() -> new CustomerException("积分名称字段未添加"));
//        Optional.ofNullable(integral.getTotal()).orElseThrow(() -> new CustomerException("总分字段未添加"));
//        Optional.ofNullable(integral.getgIntegralId()).orElseThrow(() -> new CustomerException("积分对应的国贸区的ID字段未添加"));
//        Optional.ofNullable(integral.getgIntegralName()).orElseThrow(() -> new CustomerException("积分对应的国贸区的名称字段未添加"));
        Date now = new Date();
        integral.setCreateDate(now);
        integral.setUpdateDate(now);
        return integralService.addIntergral(integral);
    }

    @GetMapping("/v1/integral/{integralId}")
    public ServiceResponse getIntegral(@PathVariable("integralId") Long id) {
        return integralService.getIntegral(id);
    }

    @PutMapping("/v1/integral/{integralId}")
    public ServiceResponse updateIntegral(@PathVariable("integralId") Long id, @RequestBody Integral integral) {
//        Optional.ofNullable(integral.getUpdateBy()).orElseThrow(() -> new CustomerException("更改人未添加"));
//        integral.setUpdateDate(new Date());
        integral.setId(id);
        integral.setUpdateDate(new Date());
        return integralService.editIntegral(integral);
    }


}
